package com.qcby.util;

import com.qcby.entity.Question;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        String questions = "{\n" +
                "  \"1\": {\n" +
                "    \"content\": \"下列哪个元素是碱金属？\",\n" +
                "    \"type\": \"选择题\",\n" +
                "    \"score\": 2,\n" +
                "    \"options\": [\"A. 铁\", \"B. 钠\", \"C. 碳\", \"D. 氧\"]\n" +
                "  },\n" +
                "  \"2\": {\n" +
                "    \"content\": \"水的化学式是______。\",\n" +
                "    \"type\": \"填空题\",\n" +
                "    \"score\": 3,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"3\": {\n" +
                "    \"content\": \"氧气是无色无味的气体。\",\n" +
                "    \"type\": \"判断题\",\n" +
                "    \"score\": 1,\n" +
                "    \"options\": [\"正确\", \"错误\"]\n" +
                "  },\n" +
                "  \"4\": {\n" +
                "    \"content\": \"计算0.1mol/L的NaOH溶液的pH值。\",\n" +
                "    \"type\": \"计算题\",\n" +
                "    \"score\": 10,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"5\": {\n" +
                "    \"content\": \"下列哪个是过渡金属？\",\n" +
                "    \"type\": \"选择题\",\n" +
                "    \"score\": 2,\n" +
                "    \"options\": [\"A. 钙\", \"B. 铜\", \"C. 铝\", \"D. 硅\"]\n" +
                "  },\n" +
                "  \"6\": {\n" +
                "    \"content\": \"标准状态下，1摩尔任何气体所占的体积约为______升。\",\n" +
                "    \"type\": \"填空题\",\n" +
                "    \"score\": 3,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"7\": {\n" +
                "    \"content\": \"酸能使蓝色石蕊试纸变红。\",\n" +
                "    \"type\": \"判断题\",\n" +
                "    \"score\": 1,\n" +
                "    \"options\": [\"正确\", \"错误\"]\n" +
                "  },\n" +
                "  \"8\": {\n" +
                "    \"content\": \"简述化学平衡的定义及其特征。\",\n" +
                "    \"type\": \"简答题\",\n" +
                "    \"score\": 5,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"9\": {\n" +
                "    \"content\": \"下列哪个是共价化合物？\",\n" +
                "    \"type\": \"选择题\",\n" +
                "    \"score\": 2,\n" +
                "    \"options\": [\"A. NaCl\", \"B. H2O\", \"C. MgO\", \"D. KBr\"]\n" +
                "  },\n" +
                "  \"10\": {\n" +
                "    \"content\": \"化学反应速率通常用______表示。\",\n" +
                "    \"type\": \"填空题\",\n" +
                "    \"score\": 3,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"11\": {\n" +
                "    \"content\": \"催化剂在反应前后质量和化学性质不变。\",\n" +
                "    \"type\": \"判断题\",\n" +
                "    \"score\": 1,\n" +
                "    \"options\": [\"正确\", \"错误\"]\n" +
                "  },\n" +
                "  \"12\": {\n" +
                "    \"content\": \"计算5g NaOH溶解在100mL水中所得溶液的质量分数。\",\n" +
                "    \"type\": \"计算题\",\n" +
                "    \"score\": 10,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"13\": {\n" +
                "    \"content\": \"下列哪个是放热反应？\",\n" +
                "    \"type\": \"选择题\",\n" +
                "    \"score\": 2,\n" +
                "    \"options\": [\"A. 冰融化\", \"B. 水蒸发\", \"C. 燃烧反应\", \"D. 光合作用\"]\n" +
                "  },\n" +
                "  \"14\": {\n" +
                "    \"content\": \"简述勒夏特列原理的主要内容。\",\n" +
                "    \"type\": \"简答题\",\n" +
                "    \"score\": 5,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"15\": {\n" +
                "    \"content\": \"原子核由质子和______组成。\",\n" +
                "    \"type\": \"填空题\",\n" +
                "    \"score\": 3,\n" +
                "    \"options\": null\n" +
                "  },\n" +
                "  \"16\": {\n" +
                "    \"content\": \"所有酸都含有氧元素。\",\n" +
                "    \"type\": \"判断题\",\n" +
                "    \"score\": 1,\n" +
                "    \"options\": [\"正确\", \"错误\"]\n" +
                "  },\n" +
                "  \"17\": {\n" +
                "    \"content\": \"下列哪个是强电解质？\",\n" +
                "    \"type\": \"选择题\",\n" +
                "    \"score\": 2,\n" +
                "    \"options\": [\"A. 醋酸\", \"B. 氨水\", \"C. 氯化钠\", \"D. 乙醇\"]\n" +
                "  }\n" +
                "}";
        List<Question> questionList = new ArrayList<>();
        JSONObject jsonObject1 = new JSONObject(questions);
        for(int i = 0;i<jsonObject1.length();i++){
            Question question = new Question();
            question.setContent(jsonObject1.getJSONObject(String.valueOf(i+1)).getString("content"));
            question.setType(jsonObject1.getJSONObject(String.valueOf(i+1)).getString("type"));
            question.setScore(jsonObject1.getJSONObject(String.valueOf(i+1)).getInt("score"));
            if (jsonObject1.getJSONObject(String.valueOf(i+1)).isNull("options")||jsonObject1.getJSONObject(String.valueOf(i+1)).get("options")== null) {
                question.setOptions(null);  // 填空题、简答题等无选项
            } else {
                JSONArray optionsArray = jsonObject1.getJSONObject(String.valueOf(i+1)).getJSONArray("options");
                question.setOptions(optionsArray.toString());  // 将 JSONArray 转为字符串
            }
            question.setEid(1);
            questionList.add(question);
            System.out.println(question);
        }
    }
}
